﻿Partial Public Class TableViewerControl

    Private m_nullstyles_per_table As New Dictionary(Of String, CellStyleDictionary)
    Private m_nullstyles As CellStyleDictionary = Nothing

    Public Sub New()
        InitializeComponent()
        MyInitialize()
    End Sub

    Private Sub MyInitialize()
        If System.ComponentModel.DesignerProperties.GetIsInDesignMode(Me) Then Return

        Dim dgv As System.Windows.Forms.DataGridView = WindowsFormsHost1.Child
        AddHandler dgv.CellFormatting, AddressOf dgv_CellFormatting
        LoadTablesListbox()
    End Sub

    Private Sub LoadTablesListbox()
        lbxTables.Items.Add(My.Settings.TABLE_Photo)
        lbxTables.Items.Add(My.Settings.TABLE_Album)
        lbxTables.Items.Add(My.Settings.TABLE_TimeZone)
        lbxTables.Items.Add(My.Settings.TABLE_City)
        lbxTables.Items.Add(My.Settings.TABLE_County)
        lbxTables.Items.Add(My.Settings.TABLE_State)
        lbxTables.Items.Add(My.Settings.TABLE_Metro)
    End Sub


    Private Sub dgv_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)
        DataGridUtils.CellFormatting(WindowsFormsHost1.Child, m_nullstyles, sender, e)
    End Sub

    Private Sub lbxTables_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbxTables.SelectionChanged
        If lbxTables.SelectedIndex < 0 Then Return

        Dim table As String = CType(lbxTables.SelectedItem, String)

        If m_nullstyles_per_table.ContainsKey(table) Then
            m_nullstyles = m_nullstyles_per_table.Item(table)
        Else
            m_nullstyles = New CellStyleDictionary
            m_nullstyles_per_table.Add(table, m_nullstyles)
        End If

        Try
            Dim top As Integer = 0
            If table = My.Settings.TABLE_City Or _
               table = My.Settings.TABLE_Metro Then top = 1000
            Dim dt As DataTable = GetDataTable(table, g_connection, top)
            If dt IsNot Nothing Then DataGridUtils.UpdateGrid(WindowsFormsHost1.Child, dt, False)

        Catch ex As Exception
            ReportException(ex)
        End Try

    End Sub

End Class
